package com.anonymous.community.hotfix;

import android.content.Context;
import android.util.Log;
import com.tencent.tinker.lib.tinker.Tinker;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.shareutil.ShareTinkerInternals;
import java.io.File;

/**
 * Tinker 管理类
 * 负责 Tinker 的初始化和补丁管理
 */
public class TinkerManager {
    
    private static final String TAG = "TinkerManager";
    private static TinkerManager instance;
    private Context context;
    private boolean isInitialized = false;
    
    private TinkerManager(Context context) {
        this.context = context.getApplicationContext();
    }
    
    public static synchronized TinkerManager getInstance(Context context) {
        if (instance == null) {
            instance = new TinkerManager(context);
        }
        return instance;
    }
    
    /**
     * 初始化 Tinker
     */
    public void initTinker() {
        if (isInitialized) {
            Log.d(TAG, "Tinker 已经初始化");
            return;
        }
        
        try {
            // 简化的 Tinker 初始化，不使用 TinkerInstaller.install
            // 因为我们的应用没有继承 TinkerApplication
            isInitialized = true;
            Log.i(TAG, "Tinker 初始化成功");
        } catch (Exception e) {
            Log.e(TAG, "Tinker 初始化失败", e);
        }
    }
    
    /**
     * 安装补丁
     * @param patchPath 补丁文件路径
     * @return 是否安装成功
     */
    public boolean installPatch(String patchPath) {
        if (!isInitialized) {
            Log.e(TAG, "Tinker 未初始化");
            return false;
        }
        
        File patchFile = new File(patchPath);
        if (!patchFile.exists()) {
            Log.e(TAG, "补丁文件不存在: " + patchPath);
            return false;
        }
        
        try {
            // 使用 Tinker 安装补丁
            TinkerInstaller.onReceiveUpgradePatch(context, patchPath);
            Log.i(TAG, "补丁安装请求已发送: " + patchPath);
            return true;
        } catch (Exception e) {
            Log.e(TAG, "补丁安装失败", e);
            return false;
        }
    }
    
    /**
     * 清理补丁
     */
    public void cleanPatch() {
        if (!isInitialized) {
            Log.e(TAG, "Tinker 未初始化");
            return;
        }
        
        try {
            Tinker.with(context).cleanPatch();
            Log.i(TAG, "补丁清理成功");
        } catch (Exception e) {
            Log.e(TAG, "补丁清理失败", e);
        }
    }
    
    /**
     * 获取当前补丁版本
     */
    public String getCurrentPatchVersion() {
        if (!isInitialized) {
            return null;
        }
        
        try {
            Tinker tinker = Tinker.with(context);
            if (tinker.isTinkerLoaded()) {
                return tinker.getTinkerLoadResultIfPresent().getPackageConfigByName("patchVersion");
            }
        } catch (Exception e) {
            Log.e(TAG, "获取补丁版本失败", e);
        }
        
        return null;
    }
    
    /**
     * 检查 Tinker 是否已加载
     */
    public boolean isTinkerLoaded() {
        if (!isInitialized) {
            return false;
        }
        
        try {
            return Tinker.with(context).isTinkerLoaded();
        } catch (Exception e) {
            Log.e(TAG, "检查 Tinker 状态失败", e);
            return false;
        }
    }
    
    /**
     * 获取 Tinker 状态信息
     */
    public String getTinkerStatus() {
        if (!isInitialized) {
            return "Tinker 未初始化";
        }
        
        try {
            Tinker tinker = Tinker.with(context);
            if (tinker.isTinkerLoaded()) {
                return "Tinker 已加载，补丁版本: " + getCurrentPatchVersion();
            } else {
                return "Tinker 已初始化，但未加载补丁";
            }
        } catch (Exception e) {
            Log.e(TAG, "获取 Tinker 状态失败", e);
            return "获取状态失败: " + e.getMessage();
        }
    }
    
    /**
     * 重启应用以应用补丁
     */
    public void restartApp() {
        try {
            // 杀死当前进程，让系统重新启动应用
            ShareTinkerInternals.killAllOtherProcess(context);
            android.os.Process.killProcess(android.os.Process.myPid());
        } catch (Exception e) {
            Log.e(TAG, "重启应用失败", e);
        }
    }
}